<template>
    <!--菜单管理-->
    <div class="category">
        <ToolBar>
            <el-button type="primary" icon="el-icon-plus" size="small" @click="visible=true">添加菜单</el-button>
        </ToolBar>
        <!--菜单本单-->
        <el-tree :data="menus">
            <div slot-scope="{data,node}" class="wrap" @mouseover="mouseover(data.id)" @mouseout="showOpt=0">
                <div class="title">
                    {{data.menu_name}}
                </div>
                <div class="buttonWrap" v-show="showOpt==data.id?true:false">
                    <el-button type="mini" size="mini" circle icon="el-icon-caret-top" v-show="node.previousSibling" @click.stop="sort(data.id,'up')"></el-button>
                    <el-button type="mini" size="mini" circle icon="el-icon-caret-bottom" v-show="node.nextSibling" @click.stop="sort(data.id,'down')"></el-button>
                    <el-button type="primary" size="mini" @click.stop="edit(data)" style="backgroundColor:rgb(238, 250, 255);color:#3C8DBC">编辑</el-button>
                    <el-button type="danger" size="mini" @click.stop="del(data.id)" style="backgroundColor:rgb(255,255,255);color:#f56c6c">删除</el-button>
                    <el-switch v-model="data.status" active-value="1" inactive-value="0" @change="cutStatus(data.id,data.status)"></el-switch>
                </div>
            </div>
        </el-tree>
        <!--添加菜单-->
        <el-dialog :visible.sync='visible'>
            <div slot="title">
                <span style="color: #3c8dbc">- </span>添加菜单
                <span style="color: #3c8dbc"> -</span>
            </div>
            <el-form :model="form" status-icon label-position="left" ref="form" :rules="rules">
                <el-form-item label="名称" label-width="70px" prop="rootName">
                    <el-input v-model="form.rootName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="图标" label-width="70px" prop="rootIcon">
                    <el-select v-model="form.rootIcon" placeholder="请选择图标">
                        <el-option v-for="(val,index) in icon" :key="index" :label="val.label" :value="val.value">
                            <span style="float: left">{{ val.label }}</span>
                            <span style="float: right; color: #8492a6; font-size: 13px">
                                <i :class="val.value"></i>
                            </span>
                        </el-option>
                    </el-select>
                    <i :class="form.rootIcon" id="rootIcon"></i>
                </el-form-item>
                <el-form-item label="路由" label-width="70px" prop="rootRouter">
                    <el-input v-model="form.rootRouter" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="是否显示" label-width="70px" prop="rootStatus">
                    <el-radio v-model="form.rootStatus" label='1'>显示</el-radio>
                    <el-radio v-model="form.rootStatus" label='0'>不显示</el-radio>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="info" size="mini" @click="rootCancel('form')">取消</el-button>
                <el-button type="primary" size="mini" @click="rootSave('form')">添加</el-button>
            </div>
        </el-dialog>

        <!--编辑菜单-->
        <el-dialog :visible.sync='editVisible'>
            <div slot="title">
                <span style="color: #3c8dbc">- </span>编辑
                <span style="color: #3c8dbc"> -</span>
            </div>
            <el-form :model="form" status-icon label-position="right" ref="form" :rules="rules">
                <el-form-item label="名称" label-width="70px" prop="editName">
                    <el-input v-model="form.editName" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="info" size="mini" @click="editCancel('form')">取消</el-button>
                <el-button type="primary" size="mini" @click="editSave('form')">保存</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import ToolBar from '@/components/ToolBar.vue';
export default {
    components: {
        ToolBar
    },
    data() {
        return {
            menus: [],
            visible: false,
            editVisible: false,
            form: {
                rootName: '',
                rootIcon: '',
                rootRouter: '/',
                rootStatus: '1',
                editName: '',
            },

            editId: 0,
            loading: false,
            showOpt: 0,
            icon: [
                {
                    label: '信息图标',
                    value: 'el-icon-info'
                },
                {
                    label: '文档图标',
                    value: 'el-icon-document'
                },
                {
                    label: '消息图标',
                    value: 'el-icon-message'
                },
                {
                    label: '菜单图标',
                    value: 'el-icon-menu'
                },
                {
                    label: '设置图标',
                    value: 'el-icon-setting'
                },
            ],
            rules: {
                rootName: [
                    { required: true, message: '请输入菜单名称', trigger: 'blur' },
                    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
                ],
                rootIcon: [
                    { required: true, message: '请选择图标', trigger: 'blur' },
                ],
                rootRouter: [
                    { required: true, message: '请输入路由', trigger: 'blur' },
                ],
                editName: [
                    { required: true, message: '请输入菜单名称', trigger: 'blur' },
                    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
                ],
            }
        }
    },
    created() {
        this.getMenus();
    },
    methods: {
        getMenus() {
            this.loading = true;
            this.axios.get('/menus').then((ret) => {
                let { data, error } = ret.data;
                this.menus = data;
                this.loading = false;
            }).catch(() => { })
        },
        rootSave(form) {
            this.$refs[form].validate((valid) => {
                if (valid) {
                    this.axios.post('/menus/add', {
                        menu_name: this.form.rootName,
                        icon: this.form.rootIcon,
                        status: this.form.rootStatus,
                        router: this.form.rootRouter,
                        parent_id: 0,
                    }).then((ret) => {
                        this.getMenus();
                        this.visible = false;
                        this.form.rootName = '';
                        this.$root.$emit('updateMenus');
                    }).catch(() => { })
                } else {
                    this.$message.error('错了哦，请正确填写表单内容');
                }
            });
        },
        rootCancel(form) {
            this.$refs[form].resetFields();
            this.visible = false;
        },
        edit(data) {
            this.editVisible = true;
            this.editId = data.id;
            this.form.editName = data.menu_name;
        },
        editSave(form) {
            this.$refs[form].validate((valid) => {
                if (valid) {
                    this.axios.put('/menus/edit/' + this.editId, {
                        menu_name: this.form.editName
                    }).then((ret) => {
                        this.getMenus();
                        this.editVisible = false;
                        this.$root.$emit('updateMenus');
                    }).catch(() => { })
                } else {
                    this.$message.error('错了哦，请正确填写表单内容');
                }
            });
        },
        editCancel(form) {
            this.$refs[form].resetFields();
            this.editVisible = false;
        },
        del(id) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                center: true
            }).then(() => {
                this.axios.delete('/menus/del/' + id).then(() => {
                    this.getMenus();
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.$root.$emit('updateMenus');
                }).catch((ret) => {
                    this.$message({
                        type: 'warning',
                        message: '请先删除子菜单'
                    });
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        },
        sort(id, dir) {
            this.axios.put('/menus/sort/' + id + '/' + dir).then(() => {
                this.getMenus();
                this.$root.$emit('updateMenus');
            }).catch(() => { })
        },
        mouseover(id) {
            this.showOpt = id;
        },
        cutStatus(id, status) {
            this.axios.put('/menus/edit/' + id, {
                status
            }).then((ret) => {
                this.$root.$emit('updateMenus');
            }).catch(() => { })
        }
    }
}
</script>
<style lang="less">
.category {
    .el-tree-node__content {
        height: 50px;
        line-height: 50px;
        .wrap {
            width: 100%;
            height: 50px;
            line-height: 50px;
            .title {
                float: left;
            }
            .buttonWrap {
                margin-right: 20px;
                float: right;
            }
        }
    }
}

.el-select {
    display: block;
}

.el-switch__core {
    margin: 0 10px;
}

#rootIcon {
    position: absolute;
    top: 12px;
    left: 100px;
}

.el-dialog__body {
    padding: 0 20px;
}
</style>
